package com.pets.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.pets.vo.CertificationResultVO;
import com.pets.vo.IdCardResultVO;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * 实名认证工具类
 */
public class CertificationUtil {

    public static boolean aLiCertification(String cardNo, String realName) {
        CertificationResultVO certificationResultVO = new CertificationResultVO();
        //设置阿里云请求所需要的请求头，appcode从阿里云控制台获得
        Map<String, String> headers = new HashMap<>();
        headers.put("Authorization", "APPCODE " + "607820d40a2b49dd96a7c02232e952fe");
        //使用httpUtil工具类调用实名认证接口
        String results = HttpsUtil.sendHttpsRequest("http://zidv2.market.alicloudapi.com/idcard/VerifyIdcardv2?cardNo="+cardNo+"&realName="+realName
                , null, "GET", headers);
        //判断接口调用是否成功,成功状态为success
        if (Objects.equals(JSON.parseObject(results).getString("status"), "success")) {
            //取出返回的result字符串，转为jsonobject对象
            JSONObject jsonObjectResult = JSON.parseObject(JSON.parseObject(results).getString("result"));
            //取出返回对象中的error_code 值，判断是否为0
            String errorCode = jsonObjectResult.getString("error_code");
            if(!errorCode.equals("0")){
                return false;
            }
            //将返回的json转换为实体对象
            IdCardResultVO idCardResultVO = IdCardResultVO.fill(jsonObjectResult.getJSONObject("result"));
            return idCardResultVO.isIsok();
        }
        return false;
    }


}
